package com.ym.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.DoctorStudioDao;
import com.ym.medical.domain.entity.DoctorDetailsEntity;
import com.ym.medical.domain.entity.DoctorStudioEntity;
import com.ym.medical.domain.entity.DoctorStudioPricingEntity;
import com.ym.medical.domain.entity.StudioPersonServiceEntity;
import com.ym.medical.domain.param.DoctorStudioQueryParam;
import com.ym.medical.domain.vo.*;
import com.ym.medical.enums.CounsellingRoomCourTypeEnum;
import com.ym.medical.service.IDoctorDetailsSV;
import com.ym.medical.service.IDoctorStudioPricingSV;
import com.ym.medical.service.IDoctorStudioSV;
import com.ym.medical.service.IStudioPersonServiceSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生工作室Service业务层处理
 *
 * @author ym
 * @date 2020-08-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DoctorStudioSVImpl extends BaseSVImpl<DoctorStudioEntity, DoctorStudioDao> implements IDoctorStudioSV {

	@Autowired
	private IDoctorDetailsSV doctorDetailsSV;


	@Autowired
	private IDoctorStudioPricingSV doctorStudioPricingSV;

	@Autowired
	private IStudioPersonServiceSV studioPersonServiceSV;

	@Override
	public DoctorStudioEntity getDoctorStudio(Long doctId) {
		return this.selectOne(SelectWhere().andEqualTo(DoctorStudioEntity::getBelongPerson, doctId).end());
	}

	@Override
	public DoctorStudioStatusViewVo studioRelatedStatusView(Long id) {
		return mapper.studioRelatedStatusView(id);
	}

	@Override
	public DoctorStudioInfoVo queryDoctorStudio(Long doctId) {
		DoctorStudioInfoVo doctorStudioInfoVo = mapper.queryDoctorStudio(doctId);
		return doctorStudioInfoVo;
	}

	@Override
	public List<DoctorStudioVo> friendMedicalStudioList(String keywords, Long loginId) {
		List<DoctorStudioVo> doctorStudios = mapper.friendMedicalStudioList(keywords, loginId);
		DoctorDetailsEntity doctorDetails = doctorDetailsSV.selectById(loginId);
		// 如果是专科，查询工作室分成
		if (doctorDetails.getDoctCategory().equals(CounsellingRoomCourTypeEnum.SPECIALIZED_SUBJECT.getKey())) {
			for (DoctorStudioVo doctorStudio : doctorStudios) {
				Long selfOfDsId = doctorStudio.getSelfOfDsId();
				List<StudioPersonServiceEntity> studioPersonServices = studioPersonServiceSV.selectList(StudioPersonServiceEntity.builder().studioPersonId(selfOfDsId).build());
				doctorStudio.setSelfStudioPricings(studioPersonServices);
			}
		}
		return doctorStudios;
	}

	@Override
	public FriendWorkDetailVo friendStudioDetailOrdersCount(Long doctorId, Long dsId) {
		Map<String, Object> params = new HashMap();
		params.put("doctorId", doctorId);
		params.put("dsId", dsId);
		params.put("nowDayStart", DateUtil.beginOfDay(new Date()).toString());
		params.put("nowDayEnd", DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), 1)).toString());
		params.put("nowMonthStart", DateUtil.beginOfMonth(new Date()).toString());
		params.put("nowMonthEnd", DateUtil.endOfMonth(new Date()).toString());
		params.put("nowYearStart", DateUtil.beginOfYear(new Date()).toString());
		params.put("nowYearEnd", DateUtil.endOfYear(new Date()).toString());
		return mapper.friendStudioDetailOrdersCount(params);
	}

	@Override
	public List<FriendWorkDetailOrderVo> friendStudioDetailOrders(Long doctorId, Long dsId, String status) {
		return mapper.friendStudioDetailOrders(doctorId, dsId, status);
	}


	@Override
	public List<DoctorStudioVo> searchQueryStudioList(Long id, String keywords, Long userId) {
		List<DoctorStudioVo> doctorStudios = mapper.searchQueryStudioList(id, keywords, userId);
		//如果当前登录为专科，则设置分成信息
		DoctorDetailsEntity doctorDetails = doctorDetailsSV.selectById(userId);
		if (doctorDetails.getDoctCategory().equals(CounsellingRoomCourTypeEnum.SPECIALIZED_SUBJECT.getKey())) {
			for (DoctorStudioVo doctorStudio : doctorStudios) {
				List<DoctorStudioPricingEntity> doctorStudioPricings = doctorStudioPricingSV.queryDoctorStudioPricing(doctorStudio.getBelongPerson(), false);
				doctorStudio.setDoctorStudioPricings(doctorStudioPricings);
			}
		}
		return doctorStudios;
	}

	@Override
	public DoctorStudioEntity getCurrStudio() {
		DoctorStudioEntity doctorStudio = this.selectOne(DoctorStudioEntity.builder().belongPerson(ShiroUtil.getLoginId()).build());
		return doctorStudio;
	}


	/**
	 * 全科医生工作室
	 * @param queryParam
	 * @return
	 */
	@Override
	public List<DoctorStudioVo> generalStudioList(DoctorStudioQueryParam queryParam) {
		List<DoctorStudioVo> doctorStudioList = mapper.generalStudioList(queryParam);
		return doctorStudioList;
	}

	/**
	 * 专科医生友医工作室列表
	 * @param pageNo
	 * @param limit
	 * @param doctId
	 * @return
	 */
	@Override
	public PageInfo<DoctorStudioVo> specialistFriendDoctor(int pageNo, int limit, long doctId) {
		String keywords = null;
		PageHelper.startPage(pageNo,limit);
		List<DoctorStudioVo> doctorStudioVos = mapper.friendMedicalStudioList(keywords,doctId);
		return new PageInfo<>(doctorStudioVos);
	}
}
